//
//  XLChatSettingVC.m
//  XiangLove
//
//  Created by 李传坤 on 2018/8/22.
//  Copyright © 2018年 BAOMIHUA.COM. All rights reserved.
//

#import "XLChatSettingVC.h"
#import <ReactiveCocoa.h>
#import "XLSWTableViewCell.h"
#import <YYCache.h>
#import <YYWebImageManager.h>
#import <YYDiskCache.h>
#import "BMHAboutMeController.h"
#import "XLV2LoginViewController.h"
#import "XLBlackListVC.h"
#import "XLPersonPageViewController.h"
#import "XLReprotUserVC.h"
@interface UITableViewCell (click)
@property (nonatomic, copy) void(^clickBlock)(void);
@end

@implementation UITableViewCell (click)
- (void (^)(void))clickBlock {
    return objc_getAssociatedObject(self, @"clickBlock");
}

- (void)setClickBlock:(void (^)(void))clickBlock {
    objc_setAssociatedObject(self, @"clickBlock", clickBlock, OBJC_ASSOCIATION_COPY_NONATOMIC);
}
@end

@interface XLChatSettingVC ()<UITableViewDelegate, UITableViewDataSource,UITextFieldDelegate>{
    NSInteger switchButton;
}
@property (nonatomic, strong) UITableView     *tableView;
@property (nonatomic, strong) UISwitch        *sw;
@property (nonatomic, copy  ) NSString        *videoHint;
@property (nonatomic, strong) NSArray         *cellArray;
@property (nonatomic, strong) UITableViewCell *homePageCell;//
@property (nonatomic, strong) UITableViewCell *swithCell;//
@property (nonatomic, strong) UITableViewCell *guanzhuCell;//
@property (nonatomic, assign) BOOL        isFollow;
@property (nonatomic, assign) BOOL        isBlack;
@property (nonatomic, strong) UIButton     *followButton;
@property (nonatomic,  copy ) void(^setChatSucessBlock)(void);
@property (nonatomic,  copy ) void(^setChatFailBlock)(void);
@end

@implementation XLChatSettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    switchButton = 0;
    _isFollow = NO;_isBlack = NO;
    [self showBackButton:NULL];
    self.navigationItem.title = @"聊天设置";
    [self setupView];
    [self setupCellArray];
    [self getIsBlack];
    [self getIsFollow];
}

- (void)getIsBlack{//从im获取拉黑状态
    [[TIMFriendshipManager sharedInstance]GetBlackList:^(NSArray *friends) {
        if ([friends containsObject:_blackUserId]) {
            _isBlack = YES;
        }else{
            _isBlack = NO;
        }
        [self setupData];
    } fail:^(int code, NSString *msg) {
        
    }];
}

- (void)getIsFollow{
    @weakify(self);
    [[[XLHTTPSessionManager sharedManager] XLV2GET:@"/Api/v2/UserInfo/Isfollow" parameters:@{ @"userid":_blackUserId} resultClass:nil] subscribeNext:^(id x) {
        @strongify(self)
        NSString *string = [NSString stringWithFormat:@"%@",x];
        if ([string integerValue] == 0) {
            _isFollow = NO;
            [_followButton setTitle:@"关注" forState:UIControlStateNormal];
        }else{
            _isFollow = YES;
            [_followButton setTitle:@"已关注" forState:UIControlStateNormal];
        }
        [self setupData];
        [self.tableView reloadData];
    } error:^(NSError *error) {
        
    }];
}

- (void)setupData {
    if (_isFollow == YES) {
        [_followButton setTitle:@"已关注" forState:UIControlStateNormal];
    }else{
        [_followButton setTitle:@"关注" forState:UIControlStateNormal];
    }
    if (_isBlack == YES) {
        _sw.on = 1;
    }else{
        _sw.on = 0;
    }
    
}

#pragma mark - initUI
- (void)setupView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
    self.tableView.separatorColor = RGB_HEX(0xEEEEEE);
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.sectionHeaderHeight = 10;
    self.tableView.sectionFooterHeight = 0.01;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    _followButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _followButton.frame = CGRectMake(20, 150, ScreenWidth - 40, 45);
    [_followButton setTitle:@"关注" forState:UIControlStateNormal];
    [_followButton setBackgroundColor:RGB_HEX(0xffbb01)];
    _followButton.layer.cornerRadius = 5;
    [_followButton addTarget:self action:@selector(followUser) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_followButton];
}

- (void)followUser{
    if (_isFollow == YES) {
        [[[XLHTTPSessionManager sharedManager] XLV2POST:@"/Api/v2/UserInfo/UnFollow" parameters:@{@"FollowId":self.blackUserId,@"FansId":[XLAccountManager shareInstance].model.Id} resultClass:nil] subscribeNext:^(id x) {
            _isFollow = NO;
            [_followButton setTitle:@"关注" forState:UIControlStateNormal];
            [MBProgressHUD showContent:@"已取消关注"];
            NSLog(@"关注成功%@",x);
        } error:^(NSError *error) {
            NSLog(@"关注失败%@",error);
            
        }];
    }else{
        [[[XLHTTPSessionManager sharedManager] XLV2POST:@"/api/v2/UserInfo/Follow" parameters:@{@"FollowId":self.blackUserId,@"FansId":[XLAccountManager shareInstance].model.Id} resultClass:nil] subscribeNext:^(id x) {
            _isFollow = YES;
            [_followButton setTitle:@"已关注" forState:UIControlStateNormal];
            [MBProgressHUD showContent:@"关注成功"];
            NSLog(@"关注成功%@",x);
        } error:^(NSError *error) {
            NSLog(@"关注失败%@",error);
            
        }];
    }
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.cellArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *sectionArray = self.cellArray[section];
    return sectionArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.cellArray[indexPath.section][indexPath.row];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 44;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 10;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    UITableViewCell *cell = self.cellArray[indexPath.section][indexPath.row];
    
    if (cell.clickBlock) {
        cell.clickBlock();
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)setupCellArray {
    @weakify(self);
    //1 个人主页
    UITableViewCell *pageCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:NSStringFromClass([UITableViewCell class])];
    pageCell.selectionStyle = UITableViewCellSelectionStyleNone;
    pageCell.detailTextLabel.font = [UIFont systemFontOfSize:16];
    pageCell.detailTextLabel.textColor = RGB_HEX(0xff5f6d);
    pageCell.textLabel.font = [UIFont systemFontOfSize:16];
    pageCell.textLabel.text = @"个人主页";
    pageCell.textLabel.textColor = kBlackColor;
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {//
        pageCell.textLabel.text = @"举报";
    }
    pageCell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    pageCell.backgroundColor = kWhiteColor;
    pageCell.clickBlock = ^{
        @strongify(self);
        if ([[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {//
            XLReprotUserVC *vc = [[XLReprotUserVC alloc]init];
            vc.toUserId = _blackUserId;
            [self.navigationController pushViewController:vc animated:YES];
            return;
        }
        XLPersonPageViewController *vc = [[XLPersonPageViewController alloc] init];
        vc.userId = _blackUserId;
        [self.navigationController pushViewController:vc animated:YES];
    };
    _homePageCell = pageCell;
    
    XLSWTableViewCell *swcell = [[XLSWTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([XLSWTableViewCell class])];
    swcell.selectionStyle = UITableViewCellSelectionStyleNone;
    swcell.textLabel.font = [UIFont systemFontOfSize:16];
    swcell.textLabel.textColor = kBlackColor;
    swcell.textLabel.text = @"加入黑名单";
    swcell.accessoryType = UITableViewCellAccessoryNone;
    swcell.backgroundColor = RGB_HEX(0xffffff);
    _sw = [[UISwitch alloc] init];
    [_sw addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventTouchUpInside|UIControlEventValueChanged];
    [swcell.contentView addSubview:_sw];
    [_sw mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(swcell.contentView.mas_right).offset(-6);
        make.centerY.equalTo(swcell.contentView);
        make.size.mas_equalTo(CGSizeMake(60, 30));
    }];
    if (_isBlack == 1) {
        _sw.on = 1;
    }else{
        _sw.on = 0;
    }
    _swithCell = swcell;
//    if ([[[NSUserDefaults standardUserDefaults] objectForKey:XiangLove_IsSH] boolValue]) {//
//        self.cellArray = @[@[_swithCell]];
//        return;
//    }
    self.cellArray = @[@[_homePageCell], @[_swithCell]];
}

#pragma mark - switch Action
- (void)switchAction:(UISwitch *)sender {
    [self toSaveUserInfo];
}


//拉黑操作
- (void)toSaveUserInfo {
//    @weakify(self)
    kWeakSelf(self);
    if (_isBlack == YES) {
        //取消拉黑
        
        [[TIMFriendshipManager sharedInstance] DelBlackList:@[_blackUserId] succ:^(NSArray *friends) {
            [MBProgressHUD showContent:@"已取消拉黑"];
            weakself.sw.on = NO;
            _isBlack = NO;
        } fail:^(int code, NSString *msg) {
            
        }];
    }else{
        //拉黑
        [[TIMFriendshipManager sharedInstance] AddBlackList:@[_blackUserId] succ:^(NSArray *friends) {
            [MBProgressHUD showContent:@"已拉黑"];
            weakself.sw.on = YES;
            _isBlack = YES;
        } fail:^(int code, NSString *msg) {
            
        }];
    }
    
    
}

@end
